﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace P
{
    /// <summary>
    /// 集合帮助类
    /// </summary>
    public class ListHelper
    {
        /// <summary>
        /// 集合克隆，目前支持List、ArrayList以及字典
        /// </summary>
        /// <param name="newList"></param>
        /// <param name="oldList"></param>
        /// <returns></returns>
        public bool ListClone(object newList, object oldList)
        {
            if (newList == null || oldList == null)
            {
                return false;
            }
            ObjectHelper objectHelper=  new ObjectHelper();
            Type newType = newList.GetType();
            Type oldType = oldList.GetType();
            //判断相同类型
            if (newType == oldType)
            {
                //如果有枚举方法并且不是字典
                if (oldList is ArrayList || oldType.GetMethod("GetEnumerator") != null)
                {
                    //如果是字典
                    if (oldType.GetMethod("ContainsKey") != null)
                    {

                        foreach (var dirValue in (oldList as dynamic))
                        {
                            var keyValue = (dirValue as dynamic).Key;
                            var valueValue = (dirValue as dynamic).Value;
                            if (keyValue is string || keyValue.GetType().GetProperties().Length == 0)
                            {

                            }
                            else
                            {
                                //初始化
                                keyValue = System.Activator.CreateInstance((dirValue as dynamic).Key.GetType());
                                objectHelper.ObjectClone(keyValue, (dirValue as dynamic).Key);
                            }

                            if (valueValue is string || valueValue.GetType().GetProperties().Length == 0)
                            {

                            }
                            else
                            {
                                //初始化
                                valueValue = System.Activator.CreateInstance((dirValue as dynamic).Value.GetType());
                                objectHelper.ObjectClone(valueValue, (dirValue as dynamic).Value);
                            }
                            (newList as dynamic).Add(keyValue, valueValue);
                        }
                    }
                    //有add方法的才用，数组Array比较复杂没搞
                    else if (oldType.GetMethod("Add") != null)
                    {
                        //遍历集合取值
                        foreach (object listItem in (oldList as dynamic))
                        {
                            //如果是值类型，直接添加
                            if (listItem is string || listItem.GetType().GetProperties().Length == 0)
                            {
                                newList.GetType().GetMethod("Add").Invoke(newList, new object[] { listItem });
                            }
                            else
                            {
                                //先实例化
                                var listValue = System.Activator.CreateInstance(listItem.GetType());
                                //如果他还是集合，则递归调用自己进行赋值操作
                                if (listValue.GetType().GetMethod("GetEnumerator") != null)
                                {
                                    ListClone(listValue, listItem);
                                }
                                //否则调用对象克隆的方法进行赋值操作
                                else
                                {
                                    objectHelper.ObjectClone(listValue, listItem);
                                }
                                //将克隆好的引用对象赋给NewObj
                                newList.GetType().GetMethod("Add").Invoke(newList, new object[] { listValue });
                            }
                        }
                    }

                }
            }

            return false;
        }
    }
}
